package test.utils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;

import java.util.Set;


/**
 * kv(aa=bb&cc=dd)解析填充器 根据已有的kv模版进行填充 1.key值无增无减 2.key值有增加 3.key值有减少
 * 
 * @author yylc-Y02673
 *
 */
public class KVResloveAndFill {

	Map<String,String> keyStore;// key储存器
	Map<String, String> oldFillString; // 需要填充的kv字符串解析
	Map<String, String> newFillString;// 完成填充后的kv字符串map形式

	

	/**
	 * kv解析器
	 * 
	 * @param template
	 */
	public void reslove(String template) {
		keyStore = new KVHashMap<>();
		
		String[] split = template.split("[&]");
		for (String string : split) {
			String[] split2 = string.split("[=]");
			keyStore.put(split2[0],split2[1]);
		}
	}

	/**
	 * 需要填充的kv字符串解析
	 * 
	 * @param oldString
	 */
	public void oldFillStringReslove(String oldString) {
		this.oldFillString = new KVHashMap<>();
		String[] split = oldString.split("[&]");
		for (String string : split) {
			String[] split2 = string.split("[=]");
			oldFillString.put(split2[0], split2[1]);
		}
	}
	
	
	Set<String> lesskey = null;//模版的key相比填充的kv字符串，有缺失的key，keystore中没有此key,即需要减少的key
	Set<String> newkey = null;//模版的key相比填充的kv字符串，有新增的key，oldfillString中没有此key，即需要新增的key
	/**
	 * kv填充器 1.key值无增无减 2.key值有增加 3.key值有减少
	 */
	public String FillToNewString() {
		
		
		if (lesskey.isEmpty()&&newkey.isEmpty()) {
			
			return oldFillString.toString();
		}
		
		if (!lesskey.isEmpty()) {
			
			for (String key : lesskey) {
				oldFillString.remove(key);
			}
		}
		
		if (!newkey.isEmpty()) {
			for (String key : newkey) {
				oldFillString.put(key, keyStore.get(key));
			
			}
		}
		
		
		return oldFillString.toString();
	}
	
	
	
	

	public Set<String> getLesskey() {
		
		return lesskey;
	}

	public void setLesskey(String lesskey) {
		if (this.lesskey == null) {
			this.lesskey = new HashSet<>();
		}
		this.lesskey.add(lesskey);
	}

	public Set<String> getNewkey() {
		return newkey;
	}

	public void setNewkey(String newkey) {
		if (this.newkey == null) {
			this.newkey = new HashSet<>();
		}
		this.newkey.add(newkey);
	}

	public Map<String, String> getKeyStore() {
		return keyStore;
	}

	public void setKeyStore(Map<String, String> keyStore) {
		this.keyStore = keyStore;
	}

	public Map<String, String> getOldFillString() {
		return oldFillString;
	}

	public void setOldFillString(Map<String, String> oldFillString) {
		this.oldFillString = oldFillString;
	}

	public Map<String, String> getNewFillString() {
		return newFillString;
	}

	public void setNewFillString(Map<String, String> newFillString) {
		this.newFillString = newFillString;
	}

	public static void main(String[] args) {
		Map<String, String> map1 = new KVHashMap<>();
		map1.put("aa", "bb");
		map1.put("bb", "cc");
		// System.out.println(map1.get("aa"));
		System.out.println(map1.toString());
		Set<String> set = null;
		System.out.println(set == null);
	}

}

class KVHashMap<K, V> extends HashMap<K, V> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public String toString() {
		Iterator<Entry<K, V>> i = entrySet().iterator();
		if (!i.hasNext())
			return "";

		StringBuilder sb = new StringBuilder();
		// sb.append('{');
		for (;;) {
			Entry<K, V> e = i.next();
			K key = e.getKey();
			V value = e.getValue();
			sb.append(key == this ? "(this Map)" : key);
			sb.append('=');
			sb.append(value == this ? "(this Map)" : value);
			if (!i.hasNext())
				return sb.toString();
			sb.append('&');
		}
	}

}
